// pages/lighthouse/index.js
import {
  getBgListByTag,
  getTagBgList,
  getBgListByHits,
  getBgListByTime,
  getBgListByDistance,
  getArticleListByType
} from '../../api/index'
import {syncDianZan} from '../../utils/sync-dianzan'
import {scrollFixed} from '../../utils/scroll-fixed'
import {authUserVerify} from '../../utils/authUserVerify'
import {getUserLocation} from '../../utils/getLocation'
import {setUserInfo} from '../../utils/setUserInfo'
import {getCurrentPageUrl} from '../../utils/get-current-route'
import {shareApp} from '../../utils/shareApp'

Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    fixedFilters: false, // time, hot
    sortBy: 'time', // time, hot
    tagOptions: [],
    tagLabel: '标签',
    currentTagID: 0,
    currentHotOrder: 'desc', // desc, asc
    currentTimeOrder: 'desc', // desc, asc
    currentDistanceOrder: 'asc', // desc, asc
    isLoading: false,
    currentPage: 1,
    hasNextPage: true,
    list: [],
  },
  // 导航到search
  bindNavToSearch () {
    authUserVerify('bg', () => {
      wx.navigateTo({
        url: '/pages/search/search?type=bgt',
      })
    })
  },
  bindPublish () {
    authUserVerify('bg', () => {
      wx.navigateTo({
        url: '/pages/article-editor/index?type=bgt',
      })
    })
  },
  // 排序
  bindSortChange (e) {
    let _sort = e.currentTarget.dataset.sort
    if (_sort === 'distance') {
      getUserLocation().then(res => {
        this.sortChangeCallback(_sort)
      })
    } else {
      this.sortChangeCallback(_sort)
    }
  },
  sortChangeCallback (sort) {
    if (sort === 'tag') return
    let sortMaps = {
      hot: 'currentHotOrder',
      distance: 'currentDistanceOrder'
    }
    if (this.data.sortBy === sort) {
      if (this.data[sortMaps[sort]] === 'desc') {
        this.setData({
          [sortMaps[sort]]: 'asc'
        })
      } else {
        this.setData({
          [sortMaps[sort]]: 'desc'
        })
      }
    }
    this.setData({
      isLoading: false,
      currentPage: 1,
      hasNextPage: true,
      list: [],
      sortBy: sort
    })
    this.getList()
  },
  
  // 标签排序方式 change
  bindTagChange (e) {
    console.log(e.detail)
    let _tagItem = this.data.tagOptions[e.detail.value]
    console.log(_tagItem)
    this.setData({
      isLoading: false,
      currentPage: 1,
      tagLabel: _tagItem ? _tagItem.name : '标签',
      hasNextPage: true,
      list: [],
      currentTagID: _tagItem.id,
      sortBy: 'tag'
    })
    this.getList()
  },
  // 获取列表
  getList (params, cb) {
    let _this = this
    let _sortBy = _this.data.sortBy
    let _page = _this.data.currentPage
    let getListParam = {
      tag: {},
      hot: {views: _this.data.currentHotOrder},
      distance: {position: _this.data.currentDistanceOrder},
    }
    wx.showLoading({title: '获取数据...'})
    _this.setData({
      isLoading: true
    })
    let _params = Object.assign({
      type: 2,
      tag: _this.data.currentTagID,
      page: _page
    }, getListParam[_sortBy], params || {})
    getArticleListByType(_params).then(res => {
      let _resInfo = res.data.info
      let _list = _page === 1 ? _resInfo : _this.data.list.concat(res.data.info)
      _this.setData({
        list: _list,
        currentPage: _page + 1,
        hasNextPage: true,
        isLoading: false
      })
      wx.hideLoading()
      cb && cb()
    }).catch(err => {
      this.setData({
        hasNextPage: false,
        isLoading: false
      })
      wx.hideLoading()
      cb && cb()
    })
  },
  
  // 接收子组件点赞状态
  bindItemDianZan (e) {
    syncDianZan(this, e, 'list')
  },
  
  onShow () {
    setUserInfo(this, true)
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    // 获取标签列表
    getTagBgList().then(res => {
      let _opts = [{
        id: 0,
        name: '全部',
      }].concat(res.data.info)
      this.setData({
        tagOptions: _opts
      })
    })
    this.getList()
  },
  onPageScroll () {
    scrollFixed(this, 'fixedFilters')
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {
    this.setData({
      currentPage: 1
    })
    this.getList({page: 1}, () => {
      wx.stopPullDownRefresh()
    })
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {
    if (!this.data.hasNextPage) return
    this.getList()
  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return shareApp()
  }
})
